package com.cdsf.kennedy.user;

//import com.cdsf.bms.BmsService;
import com.cdsf.common.conf.AbstractController;
import com.cdsf.common.exception.BusinessException;
import com.cdsf.common.util.BaseJson;
import com.cdsf.common.util.Page;
import com.cdsf.kennedy.user.domain.User;
import com.cdsf.kennedy.user.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @Author: kennedy
 * @Date: Created in 15:42 2018/2/6
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController extends AbstractController {
    @Autowired
    private UserService userService;
/*    @Autowired
    private BmsService bmsService;*/
    @Autowired
    private DiscoveryClient client;

    @ApiOperation(value = "新增用户", notes = "根据User对象添加用户")
    @PostMapping("/add")
    public BaseJson insert(User user) {
        BaseJson baseJson = new BaseJson();
        // 判断是否重名
        User query = new User();
        query.setLoginName(user.getLoginName());
        User exist = userService.selectOne(query);
        assertNull(exist, "登录名已存在");
        try {
            userService.insert(user);
            baseJson.setStatus(true);
        } catch (Exception e) {
            baseJson.setStatus(false);
            throw new BusinessException(e.getMessage());
        }
        return baseJson;
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/getAll")
    public BaseJson getAll(User user, Page page) {
        BaseJson baseJson = new BaseJson();
        List<User> users = userService.getAll(user, page);
        baseJson.setStatus(true);
        baseJson.setData(users);
        return baseJson;
    }

    @ApiOperation("获取用户列表,自己编写xml")
    @GetMapping("/getAll_")
    public BaseJson getAll_(User user, Page page) {
        BaseJson baseJson = new BaseJson();
        PageInfo<User> users = userService.getAll_(user, page);
        baseJson.setStatus(true);
        baseJson.setData(users);
        return baseJson;
    }

    @ApiOperation(value = "获取BMS部门列表", notes = "该接口通过Map传参，不能够直接使用接口文档调用")
    @ApiImplicitParams({@ApiImplicitParam(name = "id"), @ApiImplicitParam(name = "body")})
    @GetMapping(value = "/bms/getAll")
    public BaseJson getBmsAll(@ApiIgnore Map<String, Object> param) {
        BaseJson baseJson = new BaseJson();
/*        try {
            List<Dept> depts = bmsService.listDept(param);
            baseJson.setStatus(true);
            baseJson.setData(depts);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return baseJson;
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{uuid}")
    public BaseJson delete(@PathVariable("uuid") String uuid) {
        BaseJson baseJson = new BaseJson();
        userService.delete(uuid);
        baseJson.setStatus(true);
        return baseJson;
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/selectByPrimaryKey/{uuid}")
    public BaseJson selectByPrimaryKey(@PathVariable("uuid") String uuid) {
        BaseJson baseJson = new BaseJson();
        User user = userService.selectByPrimaryKey(uuid);
        baseJson.setStatus(true);
        baseJson.setData(user);
        return baseJson;
    }

    @ApiOperation("修改用户信息")
    @PutMapping("/updateByPrimaryKey")
    public BaseJson updateByPrimaryKey(User user) {
        BaseJson baseJson = new BaseJson();
        userService.updateByPrimaryKey(user);
        baseJson.setStatus(true);
        baseJson.setData(user);
        return baseJson;
    }

    /**
     * paramType：参数放在哪个地方
     * header-->请求参数的获取：@RequestHeader
     * query-->请求参数的获取：@RequestParam
     * path（用于restful接口）-->请求参数的获取：@PathVariable
     * body（不常用）
     * form（不常用）
     *
     * @param userName
     * @param pwd
     * @return
     */
    @ApiOperation("Get方式传值")
    @ApiImplicitParams({@ApiImplicitParam(name = "userName", paramType = "query", value = "用户名"), @ApiImplicitParam(name = "pwd", paramType = "query")})
    @PutMapping("/getDemo")
    public BaseJson getDemo(String userName, String pwd) {
        BaseJson baseJson = new BaseJson();
        baseJson.setData(userName);
        return baseJson;
    }
}
